<template>
  <div class="home-page">
    <!-- 固定的顶部位置和搜索栏 -->
    <div class="fixed-header">
      <div class="header">
        <div class="location">{{ currentLocation }}</div>
        <div class="search-bar">
          <input type="text" placeholder="搜索" v-model="searchQuery" @keyup.enter="handleSearch">
          <!-- 添加退出按钮 -->
          <button   class="logout-btn" @click="handleLogout">退出登录</button>
        </div>
      </div>
    </div>

    <!-- 给内容区添加顶部内边距，避免被固定头部遮挡 -->
    <div class="content-wrapper">
      <!-- 服务分类导航 - 四列布局带图片图标位置 -->
      <div class="service-nav">
        <div v-for="(item, index) in Recommendations" :key="item.id" class="nav-item" @click="selectCategory(category)">
          <span class="category-name" @click="toNest">{{ item.categoryName }}</span>
        </div>
      </div>

      <!-- 活动横幅 -->
      <div class="banner" :style="bannerStyle">
        <div class="banner-title">{{ currentPromotion.title }}</div>
        <div class="banner-subtitle">{{ currentPromotion.subtitle }}</div>
        <div class="banner-actions">
          <button v-for="(action, index) in currentPromotion.actions" :key="`action-${index}`" class="banner-btn"
            @click="handlePromotionAction(action)">
            {{ action.label }}
          </button>
        </div>
      </div>

      <!-- 热门服务 -->
      <div class="section">
        <h2 class="section-title">热门服务</h2>

      </div>
      <div>
        <van-grid :column-num="3" :gutter="10">
          <van-grid-item class="grid-item" v-for="item in servciceCap" :key="item.serviceId">
            <div>
              <van-image width="80px" height="80px" fit="contain" :src="item.src" @click="toBed" />

            </div>
            <div>{{ item.serviceName }}</div>
          </van-grid-item>
        </van-grid>
      </div>
    </div>
    <div>
    </div>
  </div>
</template>

<script setup name="homepage">
import { ref, computed, onMounted } from 'vue'
import { getAllRecommendations, getService } from '../api/index';
import { useRouter } from 'vue-router';
import { useUserInfoStore } from '../stores/userInfo'
const router = useRouter()
const userInfoStore = useUserInfoStore() // 获取用户信息store
// 响应式数据
const searchQuery = ref('')
const currentLocation = ref('青山湖区')
const selectedCategory = ref(null)
const Recommendations = ref()
const servciceCap = ref()
const toBed = () => {
  router.push({ name: 'bedInstallation' })
}
const toNest = () => {
  router.push({ name: 'NavTest' })
}
// 退出登录方法
const handleLogout = () => {
  userInfoStore.initUserInfo() // 调用store中的重置方法
  router.push('/login') // 跳转到登录页
}
const getecommendations = async () => {
  let ret = await getAllRecommendations();
  Recommendations.value = ret
  // console.log(Recommendations.value[0].categoryName);
}

const getServiceCap = async () => {
  let ret = await getService();
  servciceCap.value = ret
  // console.log(servciceCap.value);
  // console.log(servciceCap.value[0].src);
}


// 当前促销活动
const currentPromotion = ref({
  title: '清凉一夏·福利来袭',
  subtitle: '家电组合清洗 空调清洗低至65元/台',
  actions: [
    { id: 1, label: '立即抢', type: 'primary' },
    { id: 2, label: '查看详情', type: 'secondary' }
  ],
  bgGradient: ['#4facfe', '#00f2fe']
});

// 计算属性
const bannerStyle = computed(() => ({
  background: `linear-gradient(to right, ${currentPromotion.value.bgGradient[0]}, ${currentPromotion.value.bgGradient[1]})`
}));


const selectCategory = (category) => {
  selectedCategory.value = category;
};

const handleSearch = () => {
  // 搜索逻辑
  console.log('搜索:', searchQuery.value);
};

const handlePromotionAction = (action) => {
  // 处理促销活动按钮点击
  console.log('促销活动:', action);
};

const selectService = (service) => {
  // 处理服务选择
  console.log('选择服务:', service);
  // 这里可以跳转到服务详情页
};

// 生命周期钩子
onMounted(() => {

  // 可以在这里添加数据获取逻辑
  getecommendations()
  getServiceCap()
});
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
}

body {
  background-color: #f8f8f8;
  color: #333;
}

.home-page {
  /* 为固定头部和底部留出空间，避免内容被遮挡 */
  padding-top: 80px;
  /* 和固定头部高度一致，可根据实际调整 */
  padding-bottom: 70px;
}

/* 退出按钮样式 */
.logout-btn {
  padding: 8px 15px;
  /* 与搜索框保持相同垂直padding */
  background: #ff4d4f;
  color: white;
  border: none;
  border-radius: 20px;
  /* 与搜索框相同的圆角 */
  cursor: pointer;
  white-space: nowrap;
  /* 防止文字换行 */
  font-size: 14px;
  /* 合适的字体大小 */
  transition: background-color 0.3s;
  /* 添加过渡效果 */
}

.service-image {
  width: calc(85vw / 3 - 20px);
  max-width: calc(85vw / 3 - 20px);
}

/* 固定头部容器 */
.fixed-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  z-index: 999;
  /* 保证在内容上方 */
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* 让内容分布更合理，可根据需求调整 */
  padding: 10px 15px;
  max-width: 1200px;
  /* 可根据页面宽度需求设置 */
  margin: 0 auto;
}

.location {
  font-weight: bold;
}

.search-bar input {
  flex: 1;
  padding: 8px 15px;
  border-radius: 20px;
  border: 1px solid #ddd;
  margin-left: 10px;
}

/* 内容区包裹层，用于设置顶部内边距避开固定头部 */
.content-wrapper {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
}

/* 服务分类四列布局 */
.service-nav {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 15px;
  margin-bottom: 15px;
  padding: 10px 0;
}

/* 图片图标容器样式 - 预留空间 */
.category-icon {
  width: 40px;
  /* 图标宽度 */
  height: 40px;
  /* 图标高度 */
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 图片图标样式 */
.icon-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  /* 保持图片比例，适应容器 */
}

.category-name {
  text-align: center;
  word-break: keep-all;
}

.banner {
  background: linear-gradient(to right, #4facfe, #00f2fe);
  padding: 15px;
  border-radius: 10px;
  color: white;
  margin-bottom: 20px;
}

.banner-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.banner-subtitle {
  font-size: 14px;
  margin-bottom: 10px;
}

.banner-actions {
  display: flex;
  gap: 10px;
}

.banner-btn {
  padding: 8px 15px;
  background: white;
  color: #4facfe;
  border: none;
  border-radius: 20px;
  font-weight: bold;
  cursor: pointer;
}

.section {
  margin-bottom: 20px;
}

.section-title {
  font-size: 16px;
  margin-bottom: 10px;
}

.service-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.search-bar input {
  width: 170px;
  flex: 1;
  padding: 8px 15px;
  border-radius: 20px;
  border: 1px solid #ddd;
  margin-right: 10px;
}
</style>